﻿///<reference path="../lib/lt_tsbb.d.ts"/>
///<reference path="../lib/blockViews/FlowView.ts"/>
class MyBlock implements lighttool.tsbb.IBBAction_Flow {
    desc: lighttool.tsbb.BBDesc;
    getDisplayName(): string {
        return this.desc.name;
    }

    //参数组
    setInParamValue(index: number, value: any): void {
    }
    setInParmaValues(values: any[]): void {
    }
    getOutParamValue(index: number): any {
        if (index == 0)
            return 100;
        return null;
    }
    getOutParamValues(): any[] {
        return [100];
    }

    //行为组
    waitcount: number = 0;
    text: HTMLAnchorElement;
    onInit(): void //进入状态
    {
        var content = document.getElementById("content");
        this.text = document.createElement("a");
        this.text.text = "当前块" + this.desc.name;
        content.appendChild(this.text);

        this.waitcount = 50;
    }
    onExit(): void //离开状态
    {

    }

    getExitState(): number {
        if (this.waitcount <= 0)
            return 0;//0号出口
        return -1;//没处理完
    }

    onUpdate(): void//表达式计算
    {
        this.text.text = "当前块" + this.desc.name + "倒计时" + this.waitcount;
        this.waitcount--;
    }

    static reg() {
        var desc = new lighttool.tsbb.BBDesc();
        desc.name = "myblock";
        desc.exitNames = ["出口0","出口1","出口3","出口4"];
        desc.inParamNames = ["number", "string", "string"];
        desc.inParamTypes = ["integer", "string", "string"];
        desc.outParamNames = ["number", "string", "string"];
        desc.outParamTypes = ["integer", "string", "string"];
        //注册块
        lighttool.tsbb.bbFactory.instance().regItem({ desc: desc, ctor: () => { return new MyBlock(); } });
    }
}
class addOne implements lighttool.tsbb.IBBAction_Expr {
    desc: lighttool.tsbb.BBDesc;
    getDisplayName(): string {
        return this.desc.name;
    }

    //参数组
    setInParamValue(index: number, value: any): void {
        if (index == 0) {
            this.value = value + 1;
        }
    }
    setInParmaValues(values: any[]): void {
        this.value = values[0] + 1;
    }
    getOutParamValue(index: number): any {
        if (index == 0)
            return this.value;
    }
    getOutParamValues(): any[] {
        return [this.value];
    }

    getReturnType(indata: any[]): string {
        return "any";

    }
    //行为组
    value: number = null;
    run(): lighttool.tsbb.exprReturnValue {
        if (this.value == null)
            return lighttool.tsbb.exprReturnValue.InParamNotReady;
        else
            return lighttool.tsbb.exprReturnValue.Succ;
    }
    static reg() {
        var desc = new lighttool.tsbb.BBDesc();
        desc.name = "addone";
        desc.exitNames = [];
        desc.inParamNames = ["number"];
        desc.inParamTypes = ["integer"];
        desc.outParamNames = ["number"];
        desc.outParamTypes = ["integer"];
        //注册块
        lighttool.tsbb.bbFactory.instance().regItem({ desc: desc, ctor: () => { return new addOne(); } });
    }
}

window.onload = () => {

    MyBlock.reg();
    addOne.reg();

    //创建画布
    let svg: SVGSVGElement = document.createElementNS("http://www.w3.org/2000/svg", "svg");
    svg.setAttribute("width", "1700");
    svg.setAttribute("height", "700");
    let flowView = document.getElementById("flowView");                     //获取 flowView div
    flowView.appendChild(svg);

    //画一个块
    let a: Flow.views.BlockView = new Flow.views.BlockView(svg);
    let b: Flow.views.BlockView = new Flow.views.BlockView(svg);
    let c: Flow.views.BlockView = new Flow.views.BlockView(svg);
    let d: Flow.views.BlockView = new Flow.views.BlockView(svg);

    //画一个button
    let btn = document.createElement("button");
    btn.style.top = "200px";
    btn.style.left = "100px";
    btn.style.height = "100px";
    btn.style.width = "100px";
    flowView.appendChild(btn);
    

    //和逻辑块关联
    let bplayer: lighttool.tsbb.bbplayer = new lighttool.tsbb.bbplayer();
    let addone = bplayer.addBlock("addone");
    let block = bplayer.addBlock("myblock");
    let begin = bplayer.getBeginBlock();
    let end = bplayer.getEndBlock();
    begin.x = 200;
    begin.y = 200;

    end.x = 300;
    end.y = 300;

    block.x = 10;
    block.y = 10;

    addone.x = 400;
    addone.y = 400;

    a.initBlock(block);
    b.initBlock(begin);
    c.initBlock(end);
    d.initBlock(addone);
    //update

    btn.onclick = () => {
        block.x = 100;
        block.y = 100;
        //block.name = "试一试";
        a.updateBloock();
    };
};
